package com.xying.service.impl;

import com.bitsinharmony.recognito.Recognito;
import com.bitsinharmony.recognito.VoicePrint;
import com.google.common.collect.Lists;
import com.xying.common.ServerResponse;
import com.xying.dao.VoiceMapper;
import com.xying.pojo.Usr;
import com.xying.pojo.Voice;
import com.xying.service.VoiceService;
import com.xying.util.JsonUtil;
import com.xying.util.RecogntioFactory;
import com.xying.vo.VoicePrintView;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2021/4/26.
 */
@Service
@Slf4j
public class VoiceServiceImpl implements VoiceService {

    @Autowired
    private VoiceMapper voiceMapper;

    private Recognito recognito = RecogntioFactory.getRecognito();

    @Override
    public ServerResponse registerVoice(MultipartFile file, String idNo, Usr usr) {
        Voice voice = voiceMapper.selectByPrimaryKey(idNo);
        if (voice != null) {
            return ServerResponse.createByErrorMessage("用户已经注册过声纹");
        }

        File f = new File(file.getOriginalFilename());
        try {
            file.transferTo(f);
        } catch (IOException e) {
            log.error("文件转化失败", e);
            return ServerResponse.createByErrorMessage("文件转化失败");
        }

        //创建声纹
        try {
            VoicePrint print = recognito.createVoicePrint(usr.getUserName() + "-" + usr.getIdNo(),  f);
            f.delete();
            if (print == null){
                return ServerResponse.createByErrorMessage("声纹转化失败");
            }
            voice = new Voice();
            voice.setIdNo(idNo);
            voice.setFeatures(print.toString());
            voice.setMeanCount(true);
            Date date = new Date();
            voice.setUpdateTime(date);
            voice.setCreateTime(date);

            int rowCount = voiceMapper.insert(voice);
            if (rowCount <= 0){
                return ServerResponse.createByErrorMessage("声纹录入失败");
            }
        } catch (UnsupportedAudioFileException e) {
            return ServerResponse.createByErrorMessage("不支持的声音类型");
        } catch (IOException e) {
            return ServerResponse.createByErrorMessage("声音处理出现问题，请重试");
        }
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse removeVoice(String idNo, String userName) {
        int rowCount = voiceMapper.deleteByPrimaryKey(idNo);
        if (rowCount <= 0) {
            return ServerResponse.createByErrorMessage("删除失败");
        }


        recognito.remove(userName + '-' +idNo);

        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse getAllVoicePrint() {
        List<Voice> voiceList = voiceMapper.listAll();
        if (voiceList.size() <= 0){
            return ServerResponse.createByErrorMessage("查找失败");
        }


        List<VoicePrintView> voicePrintViewList = Lists.newArrayList();
        for (Voice voice : voiceList){
            VoicePrintView voicePrintView = new VoicePrintView();
            voicePrintView.setKey(voice.getIdNo());
            voicePrintView.setFeature(voice.getFeatures());

            voicePrintViewList.add(voicePrintView);
        }

        //去掉feature外的引号
        String result = JsonUtil.objToString(voicePrintViewList);
        result = result.replaceAll("]\"", "]");
        result = result.replaceAll("\"\\[", "[");

        return ServerResponse.createBySuccess(result);
    }

    @Override
    public ServerResponse selectVoice(String idNo) {
        Voice voice = voiceMapper.selectByPrimaryKey(idNo);
        if (voice == null){
            return ServerResponse.createBySuccess();
        }

        return ServerResponse.createByError();
    }
}
